﻿
//--> Loth 2011 
//--> Simple loader  simgle swf or image list

package  chamans
{
	import flash.display.*;
	import flash.events.*;
	import flash.system.*;
	import flash.net.*;
	
	public class load extends Sprite 
	{
		public function load():void
		{
			//OUT=new Function();
		}
		//___________________LOADER PROGRESS
		private function inProgress(e:ProgressEvent) :void
		{
			var name:String=Ltype+Ln
			var P:int = int(e.bytesLoaded / e.bytesTotal * 100), ko:int =(e.bytesLoaded / 1024)<<0;
			if(P==100)Lsize+=ko
			Note( "Loading: "+Lname+" | " +P+"% | "+ ko +' Ko' )
		}
		private function ERROR( e:IOErrorEvent ):void
		{
			Note("IOerror "+Lname)
		}
		//_____________________IMAGE LIST
		public function IMGLIST( link:String, list:Array, F:Function, type:String=".png", direct:Boolean=false):void
		{
			FIN = F
			var loader:Loader = new Loader()
			
			Ltype=type
			Ln=list.length
			Lname="image"
			imloaded = new Array()
			imnum = new Array()
			imliste = list; 
			var url:String
			for (var i:int=0; i<list.length; ++i){
				loader=new Loader()
				loader.name=i.toString()
				url= link + list[i] + type
				if(ID!=-1) url=url+"?uniq="+ID
				loader.load(new URLRequest( url ))
				loader.contentLoaderInfo.addEventListener('complete', IMGLISTLoaded)
				loader.contentLoaderInfo.addEventListener('progress', inProgress)
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ERROR);
			}}
		private function IMGLISTLoaded(e:Event):void{
			e.target.removeEventListener('complete', IMGLISTLoaded)
			e.target.removeEventListener('progress', inProgress)
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, ERROR);
			imloaded.push(Bitmap(e.target.content).bitmapData)
			imnum.push(Number(e.target.loader.name))
			if(imloaded.length==imliste.length){replace(); FIN(imloaded);
				Listing=Lsize+" ko"
				Note(BaseTexte+"img")
			}}
		private function replace():void{
			var old:Array=imloaded
			imloaded=new Array()
			for (var j:int=0; j<imnum.length; ++j){
				for (var i:int=0; i<imnum.length; ++i){
					if (imnum[i]==j)imloaded.push(old[i])
				}}
		}
		//___________________________SWF multiClip
		public function SWF( link:String, list:Array, F:Function , Engines:Boolean=false):void{
				
			//FINSWF=F;
			var loader:Loader = new Loader()
			var context:LoaderContext = new LoaderContext();
			context.applicationDomain=ApplicationDomain.currentDomain;
			Ltype=".swf"
			Ln=list.length;
			Lname="swf"
			if(ID!=-1) link=link+"?uniq="+ID
			loader.load(new URLRequest(link), context) 
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded)
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, inProgress)
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ERROR);
			//loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,ERROR);
			function swfLoaded(e:Event):void{
				trace("completeHandler: " + e.target);
				var Info:LoaderInfo = loader.contentLoaderInfo;
				Info.removeEventListener(Event.COMPLETE, swfLoaded)
				Info.removeEventListener(ProgressEvent.PROGRESS, inProgress)
				Info.removeEventListener(IOErrorEvent.IO_ERROR, ERROR);
				Note(BaseTexte);
				var MOVIES:Array=new Array()
				for(var i:int=0; i<list.length; i++){ MOVIES.push(Info.applicationDomain.getDefinition(list[i]));
				if(i==list.length-1){F(MOVIES);
					loader.unload()
				}} 
			}}
	 	private function Note(n:String=" "):void
		{
			if(OUT!=null)OUT( n );
		}
		//_____________________var
		private var imnum:Array;
		private var imliste:Array;
		private var imloaded:Array;
		private var FIN:Function;
		private var FINSWF:Function;
		public var OUT:Function;
		public var ID:int=-1;
		private var Listing:String=" ";
		private var Lname:String=" ";
		private var Ltype:String=" ";
		private var Ln:int=0;
		private var Lsize:Number=0;
		public var BaseTexte:String;
	}}